package com.heima.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper,AdChannel> implements AdChannelService {

    @Override
    public ResponseResult PageSelect(ChannelDTO dto) {
    // 1  校验参数
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.<AdChannel>lambdaQuery();
        if ((StringUtils.isNotEmpty(dto.getName()))){
            queryWrapper.like(AdChannel::getName,dto.getName());
            }
        if(dto.getStatus()!=null){
            queryWrapper.eq(AdChannel::getStatus,dto.getStatus());
        }
        //序号升序

        queryWrapper.orderByAsc(AdChannel::getOrd);
        //分页
        Page<AdChannel> pageReq = new Page<>(dto.getPage(), dto.getSize());
        // 执行查询
        IPage<AdChannel> pageResult = this.page(pageReq, queryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), pageResult.getTotal(), pageResult.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult insertChannel(AdChannel channel) {
        // 1 参数校验
        if(channel==null||StringUtils.isEmpty(channel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 2 判断该频道是否存在
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, channel.getName()));
        if(count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在");
        }
        if(channel.getName().length()>10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能大于10个字符");
        }
        // 3 执行新增
        channel.setCreatedTime(new Date());
        save(channel);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateChannel(AdChannel channel) {
        // 参数校验
        if (channel==null||channel.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        }
        // 2 执行修改
        AdChannel c = this.getById(channel.getId());
        if(c==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道信息不存在");
        }
        // 3 校验名称唯一性
        String name = c.getName();
        if(StringUtils.isNotEmpty(name)&&!name.equals(channel.getName())){
            if (name.length()>10){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能超过10个字符");
            }
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, channel.getName()));
            if(count>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在");
            }
        }

        // 查询数据库中是否有相同的频道名称

        this.updateById(channel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        }
        AdChannel adChannel = getById(id);
        if(adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if(adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);

        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
